import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:proj_sfv/component/MyNotification.dart';
import 'package:proj_sfv/component/SimpleVideo.dart';
import 'package:proj_sfv/component/VideoEditDialog.dart';
import 'package:proj_sfv/http/requests.dart';
import 'package:proj_sfv/store/MyArguments.dart';

class VideoCollectorPage extends StatefulWidget {
  @override
  _VideoCollectorPage createState() => _VideoCollectorPage();
}

class _VideoCollectorPage extends State<VideoCollectorPage> {
  List<String> _favoriteList = [];
  MyArguments args;

  void _showFavoriteDialog(String title) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('修改收藏夹名称'),
          content: TextField(
            decoration: InputDecoration(
              hintText: title,
            ),
            onSubmitted: (value) async {
              var resp = await updateCollectorNameRequest(context, args.id, args.token, title, value);
              if (resp != null) {
                Fluttertoast.showToast(msg: "修改成功");
                setState(() {
                  _favoriteList[_favoriteList.indexOf(title)] = value;
                });
                Navigator.pop(context);
              }
            },
          ),
        );
      },
    );
  }

  void _deleteFavorite(String title) {
      showDialog(context: context, builder: (context) => MyNotification("确认要删除收藏夹吗", [
        ElevatedButton(onPressed: () async {
          var resp = await deleteCollectorRequest(context, args.id, args.token, title);
          if (resp != null) {
            setState(() {
              _favoriteList.remove(title);
            });
            Fluttertoast.showToast(msg: "删除成功");
            Navigator.pop(context, "success");
          }
        }, child: Text('确认')),
        ElevatedButton(onPressed: () => Navigator.pop(context, "cancel"), child: Text('取消'))
      ]));
  }


  @override
  void didChangeDependencies() {
    args = ModalRoute.of(context).settings.arguments as MyArguments;
    getMyCategoryListRequest(context, args.id, args.token).then((value) {
      if (value != null) {
        setState(() {
          _favoriteList = value;
        });
      }
    });
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('收藏夹列表'),
      ),
      body: ListView.builder(
        itemCount: _favoriteList.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(_favoriteList[index]),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                IconButton(
                  icon: Icon(Icons.edit),
                  onPressed: () {
                    _showFavoriteDialog(_favoriteList[index]);
                  },
                ),
                IconButton(
                  icon: Icon(Icons.delete),
                  onPressed: () {
                    _deleteFavorite(_favoriteList[index]);
                  },
                ),
              ],
            ),
            onTap: () {
              var newArgs = MyArguments.three(args.id, args.token, _favoriteList[index], VideoPageType.COLLECT);
              Navigator.pushNamed(
                context,
                "/user/video/collector/detail",
                arguments: newArgs
              );
            },
          );
        },
      ),
    );
  }
}

class FavoriteDetailPage extends StatelessWidget {
  final String title;

  FavoriteDetailPage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Text('收藏夹内容'),
      ),
    );
  }
}
